//
//  LZMyPersonInfoTableView.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/28.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyPersonInfoTableView.h"

#import "LZMyPersonInfoCommonTableViewCell.h"
#import "LZMyTableViewButtonView.h"
#import "UIImageView+WebCache.h"

#import "LZCurrentUserInfo.h"
#import "LZThirdPartyAccountModel.h"

#import "Masonry.h"

#import "LZUserModel.h"
#import "GVUserDefaults+LZProperties.h"

@interface LZMyPersonInfoTableView() <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) LZMyTableViewButtonView * buttonView;

@end

@implementation LZMyPersonInfoTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setUserInfo:(LZCurrentUserInfo *)userInfo {
    _userInfo = userInfo;
    [self reloadData];
}

#pragma
#pragma mark private method

- (void)setup {
    self.backgroundColor = UIColorFromRGB(0xf9f7f7,1);
    self.separatorColor = [UIColor clearColor];
    self.dataSource = self;
    self.delegate = self;
}

- (LZMyTableViewButtonView *)buttonView {
    
    if (_buttonView == nil) {
        _buttonView = [[LZMyTableViewButtonView alloc] initWithButtonType:LZ_MY_TABLEVIEW_BUTTON_TYPE_QUIT];
        
        WS(weakSelf);
        _buttonView.didButtonActionBlock = ^(UIButton *button){
            if (weakSelf.didButtonActionBlock) {
                weakSelf.didButtonActionBlock(button);
            }
        };
    }
    
    return _buttonView;
}

#pragma
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataArrays.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray *arr = self.dataArrays[section];
    
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LZMyPersonInfoCommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    if (!cell) {
        cell = [[LZMyPersonInfoCommonTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                        reuseIdentifier:@"CellIdentifier"];
    }
    
    [cell setThirdPartLeftImage:nil RightImage:nil];
    
    NSArray *arr = self.dataArrays[indexPath.section];
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            if(self.userInfo.userPhoto.length > 0) {
                cell.subTitleLabel.text = LZLocalizedString(@"personal_change_photo", @"");
            } else {
                cell.subTitleLabel.text = LZLocalizedString(@"personal_set", @"");
            }
            [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", APIBaseImageUrl, self.userInfo.userPhoto]] placeholderImage:[UIImage imageNamed:@"lz_mychangedefaultimage"]];
            
            cell.type = LZ_MY_INFO_CELL_TYPE_HEADIMAGE;
        }
        else if (indexPath.row == 1) {
            cell.titleLabel.text = arr[indexPath.row];
            if (self.userInfo.nickName) {
                cell.subTitleLabel.text = self.userInfo.nickName;
            }
            
            cell.type = LZ_MY_INFO_CELL_TYPE_TITLE;
        }
    }
    else {
        cell.titleLabel.text = arr[indexPath.row];
        
        if (indexPath.row == 0) {
            if (self.userInfo.password) {
                cell.subTitleLabel.text = LZLocalizedString(@"change_setting", @"");
            }
            else {
                cell.subTitleLabel.text = LZLocalizedString(@"personal_set", @"");
            }
        }
        else if (indexPath.row == 1) {
            if (self.userInfo.cellphone.length == 0) {
                cell.subTitleLabel.text = LZLocalizedString(@"personal_set", @"");
            }
            else {
                cell.subTitleLabel.text = self.userInfo.cellphone;
            }
        }
        else if (indexPath.row == 2) {
            if (self.userInfo.email.length == 0) {
                cell.subTitleLabel.text = LZLocalizedString(@"personal_set", @"");
            }
            else {
                cell.subTitleLabel.text = self.userInfo.email;
            }
        }
        else if (indexPath.row == 3) {
            if (self.userInfo.thirdParty.count == 0) {
                cell.subTitleLabel.text = LZLocalizedString(@"change_notlinked", @"");
            }
            else {
                cell.subTitleLabel.text = @"";
                UIImage *rightImage = nil;
                UIImage *leftImage = nil;
                for (int i = 0; i<self.userInfo.thirdParty.count; i++) {
                    LZThirdPartyAccountModel *thirdPartyModel = self.userInfo.thirdParty[i];
                    
                    // 第三方类型：1，Facebook；2，Twitter
                    UIImage *imgTemp = (thirdPartyModel.type == 1)?[UIImage imageNamed:@"lz_link_facebook"]:[UIImage imageNamed:@"lz_link_twitter"];
                    
                    if (i == 0) {
                        rightImage = imgTemp;
                    }
                    else if (i == 1) {
                        leftImage = imgTemp;
                    }
                }

                [cell setThirdPartLeftImage:leftImage RightImage:rightImage];
            }
            
//            lz_link_twitter   lz_link_facebook
        }

        
        cell.type = LZ_MY_INFO_CELL_TYPE_TITLE;

    }
    
    return cell;
}

#pragma
#pragma mark UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    if (section == 0) {
        UIView * v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        v.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
        return v;
    }
    
    if (section == 1) {
        return self.buttonView;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == 0) {
        return 10.0f;
    }
    else
        return 120.0f;
    
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 85.0f;
        }
        else
            return 50.0f;
    }
    else
        return 50.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(_didSelectRowBlock) {
        _didSelectRowBlock(indexPath);
    }
}

@end
